package com.zwy.fornote.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zwy.fornote.dao.ArticleLikeMapper;
import com.zwy.fornote.dao.ArticleMapper;
import com.zwy.fornote.entity.req.ArticleReq;
import com.zwy.fornote.entity.req.NewArticleReq;
import com.zwy.fornote.entity.req.UserArticleReq;
import com.zwy.fornote.entity.vo.ArticleManageVO;
import com.zwy.fornote.model.Article;
import com.zwy.fornote.model.ArticleLike;
import com.zwy.fornote.service.ArticleService;
import com.zwy.fornote.util.CurrentUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    ArticleMapper articleMapper;

    @Resource
    ArticleLikeMapper articleLikeMapper;

    @Override
    public PageInfo<Article> selectArticle(ArticleReq req) {
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<Article> articles = articleMapper.selectArticle(req.getTitle());
        PageInfo<Article> pageInfo=new PageInfo<>(articles);
        return pageInfo;
    }

    @Override
    public PageInfo<Article> getArticlesByUser(UserArticleReq req) {
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<Article> articles = articleMapper.getArticlesByUser(req.getUserId());
        PageInfo<Article> pageInfo=new PageInfo<>(articles);
        return pageInfo;
    }

    @Override
    public PageInfo<Article> getArticlesByUserCollect(UserArticleReq req) {
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<Article> articles = articleMapper.getArticlesByUserCollect(req.getUserId());
        PageInfo<Article> pageInfo=new PageInfo<>(articles);
        return pageInfo;
    }

    @Override
    public int AddArticleBrowseNum(int articleId) {
        return 0;
    }

    @Override
    public int createNewArticle(NewArticleReq req) {
        Article article=new Article();
        article.setTitle(req.getTitle());
        article.setContent(req.getContent());
        article.setBrowseNum(0);
        article.setUserId(CurrentUser.getUser().getId());
        article.setCreateTime(new Date());
        return articleMapper.insertSelective(article);
    }

    @Override
    public int collectArticle(int artId, int userId) {
        int collectOrNot = articleLikeMapper.getCollectOrNot(artId, userId);
        if(collectOrNot>0)
        {
            return -1;
        }
        ArticleLike articleLike=new ArticleLike();
        articleLike.setArticleId(artId);
        articleLike.setUserId(userId);
        return articleLikeMapper.insert(articleLike);
    }

    @Override
    public PageInfo<ArticleManageVO> getManageArticleList(ArticleReq req) {
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<ArticleManageVO> articleManageList = articleMapper.getArticleManageList(req.getTitle());
        PageInfo<ArticleManageVO> pageInfo=new PageInfo<>(articleManageList);
        return pageInfo;
    }
}
